package com.xmy.manager;

import com.xmy.dao.Impl.MasterDaoImpl;
import com.xmy.dao.Impl.PetDaoImpl;
import com.xmy.dao.Impl.PetStoreDaoImpl;
import com.xmy.dao.MasterDao;
import com.xmy.dao.PetDao;
import com.xmy.dao.PetStoreDao;
import com.xmy.entity.Master;
import com.xmy.entity.Pet;
import com.xmy.entity.PetStore;
import com.xmy.service.Buyable;
import com.xmy.service.imlp.MasterServiceImpl;
import com.xmy.service.imlp.PetStoreServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PetManager {
    Scanner input = new Scanner(System.in);
    PetDao petDao = new PetDaoImpl();
    MasterDao masterDao = new MasterDaoImpl();
    PetStoreDao psd = new PetStoreDaoImpl();
    MasterServiceImpl msi = new MasterServiceImpl();
    PetStoreServiceImpl psi = new PetStoreServiceImpl();

    /**
     * 主菜单
     */
    public void menu(){
        System.out.println("宠物商店启动");
        System.out.println("************************");
        SelPetInfo();
        System.out.println();
        System.out.println("所有宠物主人");
        System.out.println("*************************");
        SelMasterInfo();
        System.out.println();
        System.out.println("所有宠物商店");
        SelStoreInfo();
        System.out.println();
        System.out.println("请选择输入登录模式，输入1为宠物主人登录，输入2为宠物商店登录：");
        int choose = input.nextInt();
        switch (choose){
            case 1:
                System.out.println("请先登录，请您输入主人的名字：");
                String name = input.next();
                System.out.println("请您输入主人的密码：");
                String pwd = input.next();
                do {
                    Master master = msi.login(name,pwd);
                    if(master!=null){
                        System.out.println("-----恭喜您成功登录-----");
                        System.out.println("-----您的基本信息-----");
                        System.out.println("名字："+master.getLoginid());
                        System.out.println("元宝数："+master.getBalance());
                        break;
                    }else{
                        System.out.println("登录失败！请确认用户名和密码后重新输入");
                        System.out.print("名字:");
                        name = input.next();
                        System.out.print("密码:");
                        pwd = input.next();
                    }
                } while (true);
                System.out.println("登录成功您可以购买和卖出宠物,购买输入1，卖出输入2");
                System.out.println("1.购买宠物");
                System.out.println("2.卖出宠物");
                int mChoose = input.nextInt();
                masterMenu(mChoose);
                break;
            case 2:
                System.out.println("请先登录，请您输入宠物商店的名字：");
                String storeName = input.next();
                System.out.println("请您输入宠物商店的密码：");
                String storePwd = input.next();
                do {
                    PetStore petStore = psi.login(storeName,storePwd);
                    if(petStore!=null){
                        System.out.println("-----恭喜您成功登录-----");
                        System.out.println("-----宠物商店的基本信息-----");
                        System.out.println("名字："+petStore.getLoginid());
                        System.out.println("元宝数："+petStore.getBalance());
                        break;
                    }else{
                        System.out.println("登录失败！请确认用户名和密码后重新输入");
                        System.out.print("名字:");
                        storeName = input.next();
                        System.out.print("密码:");
                        storePwd = input.next();
                    }
                } while (true);
                System.out.println("登录成功您可以进行如下操作：");
                System.out.println("1.购买宠物");
                System.out.println("2.卖出宠物");
                System.out.println("3.培育宠物");
                System.out.println("4.查询待售宠物");
                System.out.println("5.查看商店结余");
                System.out.println("6.查看商店账目");
                System.out.println("7.开宠物商店");
                System.out.println("请根据需要执行的操作选择序号输入，退出请输入0");
                break;
        }
    }

    /**
     * 主人可以进行的操作
     * @param mChoose
     */
    private void masterMenu(int mChoose) {
        switch (mChoose){
            case 1:
                buyPet();
                break;
            case 2:
                sellPet();
                break;
        }
    }

    /**
     * 卖宠物
     */
    private void sellPet() {

    }

    /**
     * 查询宠物信息
     */
    public void SelPetInfo(){
        System.out.println("序号\t宠物名称");
        List<Pet> pets = petDao.SelPet();
        for (Pet pet:pets) {
            System.out.println(pet.getPetid()+"\t"+pet.getName());
        }
    }

    /**
     * 查询宠物主人信息
     */
    public void SelMasterInfo(){
        List<Master> masters =  masterDao.SelMaster();
        System.out.println("序号\t主人姓名");
        for (Master master:masters) {
            System.out.println(master.getMid()+"\t"+master.getLoginid());
        }
    }

    /**
     * 查询宠物商店信息
     */
    public void SelStoreInfo(){
        List<PetStore> stores = psd.SelPetStore();
        System.out.println("序号\t宠物商店名称");
        for (PetStore petStore:stores) {
            System.out.println(petStore.getSid()+"\t"+petStore.getLoginid());
        }
    }

    /**
     * 购买宠物
     */
    public void buyPet(){
        System.out.println("----请输入选择要购买范围：只输入选择项的序号----");
        System.out.println("1.购买库存宠物");
        System.out.println("2.购买新培育宠物");
        int choose = input.nextInt();
        switch (choose){
            case 1:
                System.out.println("---以下是库存宠物---");
                String breed = "库存";
                List<Pet> pets = psi.getPetsInStock(breed);
                System.out.println("序号\t宠物名称\t类型\t元宝数");
                for (Pet pet:pets) {
                    System.out.println(pet.getPetid()+"\t"+pet.getName()+"\t"+pet.getType()+"\t"+pet.getPrice());
                }
                System.out.println("---请选择要购买哪一个宠物，并输入选择项的序号---");
                int petid = input.nextInt();
                Pet pet = petDao.SelPetById(petid);
                msi.buy(pet);
                System.out.println("台账正确插入一条信息");
                break;
            case 2:
                break;
        }
    }
}
